﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;
using MeTalker.Aide.Mode;
using MeTalker.Aide.Service;

namespace MeTalker.Aide.Forms
{
    internal partial class frmLicense : MeForm
    {LoginProcHandler updateHandler;
        public frmLicense()
        {
            InitializeComponent();
            updateHandler= new LoginProcHandler(UpdateLicense);
            lblState.Text = "";
            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.DataSource = new List<User>(){ DataFactory.User};
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            lblState.Text = "正在更新许可...";
            updateHandler.BeginInvoke(null, null, null, null);
        }

        void UpdateLicense(object sender, byte[] b1)
        {
            try
            {
                byte[] b = HttpClient.RequestHeart(string.Format(UtilHelper.UPDATE_LICENSE_URL, UtilHelper.TYPE_USER_LICENSE, DataFactory.User.SessionId, new Random().Next(),DataFactory.User.UserName), new byte[] { 1 }, 1);
                Update(this, b);
            }
            catch
            {
                Update(sender, null);
            }
        }

        void Update(object sender, byte[] b)
        {
            LoginProcHandler del = delegate(object sender1, byte[] b1)
            {
                try
                {
                    if (b != null)
                    {
                        string xml = Encoding.UTF8.GetString(b);
                        if (xml.Equals(UtilHelper.LOGIN_FAIL)) {
                            lblState.Text = "更新失败! ";
                            return;
                        }
                        DataFactory.InitUser(xml);
                        dataGridView.DataSource = new List<User>() { DataFactory.User };
                        lblState.Text = "更新成功! ";
                        return;
                    }
                    lblState.Text = "更新失败! ";
                }
                catch
                {
                    lblState.Text = "更新失败! ";
                }
                finally {
                    btnUpdate.Enabled = true;
                }
            };
            this.BeginInvoke(del, new object[] { sender, b });
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {

        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }

        private void btnBuy_Click(object sender, EventArgs e)
        {
            MeMessageBox.ShowInformation("软件购买请联系官方客服QQ(563174509)!");
        }
    }
}
